<template>
  <div>
    <el-dialog
      :title="!dataForm.id ? '新增' : '查看'"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <!-- <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">

      <el-form-item label="产品序号" :label-width="formLabelWidth" prop="productId">
        <el-select v-model="dataForm.productId" clearable placeholder="请选择" @change="productModelChange" disabled>
          <el-option v-for="item in productModelOptions" :key="item.id" :label="item.code" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" :label-width="formLabelWidth" prop="userCompany">
        <el-input v-model.trim="dataForm.userCompany" placeholder="用户名称(用户公司)" disabled> </el-input>
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth" prop="userName">
        <el-input v-model.trim="dataForm.userName" placeholder="姓名" disabled> </el-input>
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth" prop="userPhone">
        <el-input v-model.trim="dataForm.userPhone" placeholder="电话" disabled> </el-input>
      </el-form-item>
      <el-form-item label="地址" :label-width="formLabelWidth" prop="userAddress">
        <el-input v-model.trim="dataForm.userAddress" placeholder="地址" disabled> </el-input>
      </el-form-item>
      <el-form-item label="现场问题描述" :label-width="formLabelWidth" prop="problemDesc">
        <el-input v-model.trim="dataForm.problemDesc" type="textarea" disabled> </el-input>
      </el-form-item>
      <el-form-item label="处理描述" :label-width="formLabelWidth" prop="handleDesc" v-if="dataForm.handleState">
        <el-input v-model.trim="dataForm.handleDesc" type="textarea" disabled> </el-input>
      </el-form-item>
      <el-form-item label="评价" :label-width="formLabelWidth" prop="evaluate" v-if="dataForm.evaluate!=''">
        <el-radio-group v-model="dataForm.evaluate">
            <el-radio :label="0" v-if="dataForm.evaluate==0">不满意</el-radio>
            <el-radio :label="1" v-if="dataForm.evaluate==1">满意</el-radio>
      </el-radio-group>
      </el-form-item>
      <el-form-item label="您认为本公司的服务需要改进的地方" :label-width="formLabelWidth" prop="suggestion" v-if="dataForm.evaluate!=''"> 
        <el-input v-model.trim="dataForm.suggestion" type="textarea" disabled> </el-input>
      </el-form-item>
    </el-form> -->
      <div class="listItem">
        <div class="list">
          <div class="item">
            <label>产品序号：</label><span>{{ dataForm.productName }}</span>
          </div>
        </div>
        <div class="list">
          <div class="item">
            <label>用户名称：</label><span>{{ dataForm.userCompany }}</span>
          </div>
        </div>
        <div class="list">
          <div class="item">
            <label>姓名：</label><span>{{ dataForm.userName }}</span>
          </div>
        </div>
        <div class="list">
          <div class="item">
            <label>电话：</label><span>{{ dataForm.userPhone }}</span>
          </div>
        </div>
        <div class="list">
          <div class="item">
            <label>地址：</label><span>{{ dataForm.userAddress }}</span>
          </div>
        </div>
        <div class="list">
          <div class="items">
            <label>现场问题描述:</label>
            <div class="texareastyle">{{ dataForm.problemDesc }}</div>
          </div>
          <!-- <div class="items"><label>现场问题描述：</label><div class="texareastyle">现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述现场问题描述</div></div> -->
        </div>
        <div class="list" v-if="dataForm.handleState == 1">
          <div class="item">
            <label>处理人：</label><span>{{ dataForm.handUserName }}</span>
          </div>
        </div>
        <div class="list" v-if="dataForm.handleState == 1">
          <div class="item">
            <label>故障类型：</label><span>{{ dataForm.faultTypeNames }}</span>
          </div>
        </div>
        <div class="list itemfile" v-if="dataForm.handleState == 1">
          <div class="item"><label>图片：</label></div>
          <div class="imgList">
            <div v-for="item in fileList" class="imgItem">
              <img
                v-if="!item.fileType || item.fileType == 'image'"
                :src="url + item.filePath"
                alt=""
              />
              <div v-else-if="item.fileType == 'video'">
                <video
                  style="width: 100%; height: 100%"
                  disabled="false"
                  controls="false"
                  :src="item.response ? item.url : url + item.filePath"
                >
                  <source
                    :src="item.response ? item.url : url + item.filePath"
                    type="video/mp4"
                  />
                  <source
                    :src="item.response ? item.url : url + item.filePath"
                    type="video/mov"
                  />
                  <source
                    :src="item.response ? item.url : url + item.filePath"
                    type="video/avi"
                  />
                </video>
              </div>

              <span class="imgitem-actions">
                <span
                  class="imgitem-preview"
                  @click="handlePictureCardPreview(item)"
                >
                  <i class="el-icon-zoom-in"></i>
                </span>
                <!-- <span class="imgitem-delete" @click="handleDownloadImg(item)">
                  <i class="el-icon-download"></i>
                </span> -->
              </span>
            </div>
          </div>
        </div>
        <div class="list" v-if="dataForm.handleState == 1">
          <div class="items">
            <label>处理描述:</label>
            <div class="texareastyle">{{ dataForm.handleDesc }}</div>
          </div>
        </div>
        <div class="list" v-if="dataForm.evaluate != ''">
          <div class="item">
            <label>评价：</label
            ><span v-if="dataForm.evaluate == 0">不满意</span
            ><span v-if="dataForm.evaluate == 1">满意</span>
          </div>
        </div>
        <div class="list" v-if="dataForm.evaluate != ''">
          <div class="items">
            <label>您认为本公司的服务需要改进的地方：</label>
            <div class="texareastyle">{{ dataForm.suggestion }}</div>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false"
            >关 闭</el-button
          >
          <!-- <el-button size="small" type="primary" @click="dataFormSubmit()" :disabled="isDisabled">确 定</el-button> -->
        </span>
      </template>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisibleImg">
      <img
        style="max-width: 100%; margin: 0 auto; display: black"
        :src="dialogImageUrl"
        alt=""
      />
    </el-dialog>
    <el-dialog
      :visible.sync="dialogVisibleVideo"
      :append-to-body="true"
      width="50%"
      style="height: 100%"
    >
      <div style="height: 60%">
        <!-- <img style="max-width: 100%;" :src="dialogImageUrl" alt=""> -->
        <video
          controls
          style="max-width: 100%"
          :autoplay="true"
          :src="dialogImageUrl"
          :show-fullscreen-btn="false"
        ></video>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import { getToken } from "@/utils/auth";
import { treeDataTranslate } from "../../utils/treeDataTranslate.js";
export default {
  emits: ["refreshDataList"],
  data() {
    return {
      visible: false,
      dialogFormVisible: false,
      isDisabled: false,
      isval: false,
      isupdate: 0,
      istype: "",
      dialogVisibleImg: false,
      dialogVisibleVideo: false,
      dialogImageUrl: "",
      dataForm: {
        id: 0,
        productId: "",
        productName: "",
        userCompany: "", //用户名称（用户公司）
        userName: "", //姓名
        userPhone: "", //电话
        userAddress: "", //地址
        problemDesc: "", //现场问题描述
        suggestion: "", //您认为本公司的服务需要改进的地方
        appendixUrl: "", //附件路径
        appendixName: "", //附件名称
        evaluate: 1,
        handleDesc: "",
        handUserName: "",
        faultTypeNames: "",
      },
      productModelOptions: [],
      url: process.env.VUE_APP_BASE_API,
      headers: {
        token: getToken(), //从cookie里获取token，并赋值  Authorization ，而不是token
      },
      dataRule: {
        evaluate: [{ required: true, message: "评价必填" }],
        driverPhone: [{ required: true, message: "司机联系电话必填" }],
        licenseNumber: [{ required: true, message: "车牌号必须填写" }],
      },
      formLabelWidth: "150px",
      visible: false,
      handleState: 0,
      fileList: [],
      faultTypeList: [],
      // url: process.env.VUE_APP_BASE_API,
    };
  },
  created() {},
  methods: {
    async init(id) {
      this.dataForm.id = id || 0;
      this.dialogFormVisible = true;
      await this.Sevice({
        url: "/syn/Feedback/faultSelect",
        method: "get",
      }).then((res) => {
        // console.log(res);
        if (res.code != 200) {
        } else {
          this.faultTypeList = treeDataTranslate(res.select, "id");
        }
      });
      this.$nextTick(() => {
        // this.$refs["dataForm"].resetFields();
        this.dataForm.productModel = "";
        this.productModelOptions = [];
        this.fileList = [];
        this.Sevice({
          url: "/product/list",
          method: "get",
        }).then(async (res) => {
          // console.log(res);
          if (res.code != 200) {
            // this.$message.error(res.data.msg)
          } else {
            this.productModelOptions = res.list;
          }
        });
        if (!this.dataForm.id) {
          // 新增
          // this.treeListTreeSetCurrentNode()
        } else {
          //获取详情
          this.Sevice({
            url: "/syn/Feedback/info/" + this.dataForm.id,
            method: "get",
          }).then(async (res) => {
            // console.log(res);
            if (res.code != 200) {
              // this.$message.error(res.data.msg)
            } else {
              this.dataForm.id = res.info.id;
              this.dataForm.problemDesc = res.info.problemDesc;
              this.dataForm.productName = res.info.productName;
              this.dataForm.productId = res.info.productId;
              this.dataForm.suggestion = res.info.suggestion;
              this.dataForm.userAddress = res.info.userAddress;
              this.dataForm.userCompany = res.info.userCompany;
              this.dataForm.userName = res.info.userName;
              this.dataForm.userPhone = res.info.userPhone;
              this.dataForm.handleState = res.info.handleState;
              this.dataForm.handleDesc = res.info.handleDesc;
              this.dataForm.evaluate = res.info.evaluate ? 1 : 0;
              this.dataForm.handUserName = res.info.handUserName;
              if (res.info.appendixUrl) {
                let appendixUrl = res.info.appendixUrl.split(",");
                let appendixName = res.info.appendixName.split(",");
                let list = [];
                for (let i = 0; i < appendixUrl.length; i++) {
                  if (
                    appendixUrl[i].indexOf(".jpg") > -1 ||
                    appendixUrl[i].indexOf(".png") > -1 ||
                    appendixUrl[i].indexOf(".jpeg") > -1
                  ) {
                    list.push({
                      filePath: appendixUrl[i],
                      fileType: "image",
                      fileName: appendixName[i],
                    });
                  }
                  if (
                    appendixUrl[i].indexOf(".mp4") > -1 ||
                    appendixUrl[i].indexOf(".mov") > -1 ||
                    appendixUrl[i].indexOf(".avi") > -1
                  ) {
                    list.push({
                      filePath: appendixUrl[i],
                      fileType: "video",
                      fileName: appendixName[i],
                    });
                  }
                }
                this.fileList = list;
                console.log(this.fileList);
              }
              this.dataForm.faultTypeNames = this.getTreeIds(
                this.faultTypeList,
                Number(res.info.faultTypeIds)
              );
            }
          });
        }
      });
    },
    getTreeIds(tree, nodeId, config) {
      console.log(tree, nodeId);
      const { children = "children", id = "id" } = config || {};
      const toFlatArray = (tree, parentId) => {
        return tree.reduce((t, _) => {
          const child = _[children];
          return [
            ...t,
            parentId ? { ..._, parentId } : _,
            ...(child && child.length ? toFlatArray(child, _[id]) : []),
          ];
        }, []);
      };
      const getIds = (flatArray) => {
        let ids = [nodeId];
        let names = [];
        let child = flatArray.find((_) => _[id] == nodeId);
        if (child && child.parentId) {
          names = [child.faultName];
        } else {
          if (child) {
            names = [child.faultName];
          }
        }

        while (child && child.parentId) {
          ids = [child.parentId, ...ids];
          child = flatArray.find((_) => _[id] == child.parentId);
          names = [child.faultName, ...names];
        }
        // return [ids,names]
        console.log(names);
        return names.join("/");
      };
      return getIds(toFlatArray(tree));
    },
    productModelChange(id) {
      for (let i = 0; i < this.productModelOptions.length; i++) {
        if (this.productModelOptions[i].id == id) {
          this.dataForm.productName = this.productModelOptions[i].code;
        }
      }
    },
    handlePictureCardPreview(file) {
      // this.dialogImageUrl = file.url;
      // this.dialogVisibleImg = true;
      this.dialogImageUrl = file.response
        ? this.url + file.response.map.filePath
        : this.url + file.filePath;
      if (
        file.filePath.indexOf(".jpg") > -1 ||
        file.filePath.indexOf(".png") > -1 ||
        file.filePath.indexOf(".jpeg") > -1
      ) {
        this.dialogVisibleImg = true;
      }
      if (
        file.filePath.indexOf(".mp4") > -1 ||
        file.filePath.indexOf(".mov") > -1 ||
        file.filePath.indexOf(".avi") > -1
      ) {
        this.dialogVisibleVideo = true;
      }
    },
    handleDownloadImg(file) {
      this.fileDown(file);
    },

    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.fileList = fileList;
      } else {
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].uid == file.uid) {
            fileList.splice(i, 1);
          }
        }
        this.$message({ message: response.msg, type: "error", duration: 1000 });
      }
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    // 表单提交
    async dataFormSubmit() {
      this.$refs["dataForm"].validate(async (valid) => {
        if (valid) {
          let url = this.dataForm.id
            ? "/syn/Feedback/update"
            : "/syn/Feedback/save";
          this.isDisabled = true;
          await this.Sevice({
            url: url,
            method: "post",
            data: {
              id: this.dataForm.id || undefined,
              problemDesc: this.dataForm.problemDesc,
              productId: this.dataForm.productId,
              productName: this.dataForm.productName,
              suggestion: this.dataForm.suggestion,
              userAddress: this.dataForm.userAddress,
              userCompany: this.dataForm.userCompany,
              userName: this.dataForm.userName,
              userPhone: this.dataForm.userPhone,
            },
          })
            .then((res) => {
              if (res && res.code == 200) {
                this.dialogFormVisible = false;
                this.$message({
                  message: res.msg,
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.isDisabled = false;
                    this.$emit("getMenuList");
                  },
                });
              } else {
                this.isDisabled = false;
              }
            })
            .catch((error) => {
              console.log(error);
              this.isDisabled = false;
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.mod-menu .menu-list__input > .el-input__inner,
.mod-menu .icon-list__input > .el-input__inner {
  cursor: pointer;
}

.mod-menu__icon-popover {
  width: 458px;
  overflow: hidden;
}

.mod-menu__icon-inner {
  width: 478px;
  max-height: 258px;
  overflow-x: hidden;
  overflow-y: auto;
}

.mod-menu__icon-list {
  width: 458px;
  padding: 0;
  margin: -8px 0 0 -8px;
}

.mod-menu__icon-list > .el-button {
  padding: 8px;
  margin: 8px 0 0 8px;
}

.mod-menu__icon-list > .el-button > span {
  display: inline-block;
  vertical-align: middle;
  width: 18px;
  height: 18px;
  font-size: 18px;
}

.mod-menu .icon-list__tips {
  font-size: 18px;
  text-align: center;
  color: #e6a23c;
  cursor: pointer;
}

.el-tree {
  height: 200px;
  overflow-y: auto;
}
</style>
<style>
.formItem .el-form-item__label {
  float: none;
}

.item {
  display: flex;
  padding: 5px 0;
}

.item label {
  margin-right: 10px;
  min-width: 85px;
}

.item span {
  width: calc(100% - 85px);
  /* padding: 5px 0; */
  line-height: 26px;

  /* border-bottom: 1px solid #ccc; */
}

.items div {
  /* padding: 5px 0; */
  line-height: 26px;
}

.list label {
  /* padding: 5px 0; */
  line-height: 26px;
  color: #999;
}

.list {
  /* border-bottom: 1px solid #ccc; */
}

.listItem {
  /* box-shadow: 0px 0px 3px #ccc;
  padding: 10px; */
  font-family: dengxian;
}

.texareastyle {
  text-indent: 28px;
  padding: 5px 28px !important;
  border: 1px solid #ccc;
  min-height: 100px;
  border-radius: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;
}

.imgItem {
  overflow: hidden;
  background-color: #fff;
  border: 1px solid #c0ccda;
  border-radius: 6px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 148px;
  height: 148px;
  margin: 0 8px 8px 0;
  position: relative;
}

.imgItem img {
  width: 100%;
}

.imgitem-thumbnail {
  width: 100%;
  /* height: 100%; */
}

.imgitem-actions {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  cursor: default;
  text-align: center;
  color: #fff;
  opacity: 0;
  font-size: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  transition: opacity 0.3s;
  display: flex;
  justify-content: center;
  align-items: center;
}

.imgitem-actions:hover {
  opacity: 1;
}

.imgitem-preview {
  cursor: pointer;
  margin-right: 15px;
}

.imgitem-delete {
  cursor: pointer;
}
</style>
